package com.zyg.manager.controller;

import com.zyg.common.utils.FastDFSClient;
import com.zyg.common.utils.R;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping
public class UploadController {
    @PostMapping("/manager/upload")
    public R uploadFile(MultipartFile file) throws Exception {
        //得到fastdfs对象
        FastDFSClient client = new FastDFSClient("classpath:fastdfs.conf");
        //得到文件名
        String fileName = file.getOriginalFilename();
        //得到文件后缀名
        String extName = fileName.substring(fileName.lastIndexOf(".")+1);
        //开始文件上传
        String path = client.uploadFile(file.getBytes(), extName);
        //得到文件地址
        String url = "http://192.168.56.16:8080/"+path;
        //返回
        return R.ok().put("url",url);
    }
}
